//
//  LineView.m
//  WidgetTest
//
//  Created by 李明明 on 15/10/20.
//  Copyright © 2015年 李明明. All rights reserved.
//

#import "LineView.h"

@interface LineView ()

@end

@implementation LineView
{
    NSString *key;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
//    [self makeLine];
    [self makeAnimationLine];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
}
- (void)makeAnimationLine
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(self.frame.size.width * 3 / 4, self.frame.size.height / 2)];
    [path addArcWithCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) radius:20 startAngle:M_PI * 2 endAngle:0 clockwise:NO];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.strokeColor = [UIColor blueColor].CGColor;
    
    UIBezierPath *paths = [UIBezierPath bezierPath];
    [paths moveToPoint:CGPointMake(self.frame.size.width * 3 / 4, self.frame.size.height / 2)];

    [paths addArcWithCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2) radius:20 startAngle:M_PI * 2 endAngle:0 clockwise:NO];
    layer.lineWidth = 3.0f;
    layer.fillColor = [UIColor whiteColor].CGColor;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.duration = 1.0;
    animation.repeatCount = MAX_CANON;
    [layer addAnimation:animation forKey:@"lineLayerAnimations"];
    [self.layer addSublayer:layer];
    

    
    
    
    
}



- (void)makeLine
{
    self.backgroundColor = [UIColor clearColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, 100, 100);
//    CGContextClosePath(context);
//    CGContextBeginPath(context); // 如果上面的功能没有用DrawPath画出来，这句的出现就会使上面失效
    CGContextMoveToPoint(context, 100, 0);
    CGContextAddLineToPoint(context, 0, 100);
//    CGContextSetBlendMode(context, kCGBlendModeDarken);
    CGContextDrawPath(context, kCGPathFillStroke);
//    self.transform = CGAffineTransformConcat(CGAffineTransformMakeRotation(M_PI), CGAffineTransformMakeTranslation(100, 100));
//    self.transform = CGAffineTransformInvert(CGAffineTransformMakeRotation(M_PI / 2));
//    CGPoint point = CGPointApplyAffineTransform(CGPointMake(1, 1), CGAffineTransformMakeRotation(M_PI)); // 得到围绕原点做顺时针旋转的点
//    NSLog(@"new point:%f, %f", point.x, point.y);
//    CGSize size = CGSizeApplyAffineTransform(CGSizeMake(10, 20), CGAffineTransformMakeRotation(M_PI / 2)); // 得到顺时针旋转的size
//    NSLog(@"%f, %f", size.width, size.height);
    /*
     *conclusion: view的transform属性可以让view旋转平移，缩放
     */
    // create path
    CGMutablePathRef path = CGPathCreateMutable();
//    CGPathMoveToPoint(path, NULL, self.frame.origin.x + 50, self.frame.origin.y);
    CGPathAddArc(path, &CGAffineTransformIdentity, self.center.x, self.center.y, 50, 2 * M_PI, 0, YES);
//    CGPathAddArcToPoint(path, NULL, self.frame.origin.x + 100, self.frame.origin.y , self.frame.size.width, self.frame.size.height, M_PI);
//    CGPathAddArcToPoint(path, &CGAffineTransformIdentity, self.frame.origin.x + 100, self.frame.origin.y + 100, self.frame.origin.x, self.frame.origin.y, M_PI / 4);
//    CGPathAddLineToPoint(path, NULL, self.center.x + 50, self.center.y + 50);
//    CGPathAddLineToPoint(path, NULL, self.center.x, self.center.y);
//    CAAnimation *animation = [CAAnimation animation];
    CAKeyframeAnimation *animations = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animations.path = path;
    animations.duration = 1.5f;
    animations.repeatCount = MAX_CANON;
    CGPathRelease(path);
//    animations.delegate = self;
    [self.layer addAnimation:animations forKey:@"animationkey"];
//    self.layer
    
    
}









@end
